package server

import (
	"fmt"
	"github.com/gorilla/websocket"
)

type Client struct {
	ID   int
	Send chan []byte
	Conn *websocket.Conn
}

func (c *Client) Read() {
	defer func() {
		MyServer.UnRegister <- c
		c.Conn.Close()
	}()

	for {
		_, message, err := c.Conn.ReadMessage()
		if err != nil {
			fmt.Println(err)
			break
		}
		MyServer.Broadcast <- message
	}

}

func (c *Client) Write() {
	defer func() {
		MyServer.UnRegister <- c
		c.Conn.Close()
	}()

	for message := range c.Send {
		err := c.Conn.WriteMessage(websocket.TextMessage, message)

		if err != nil {
			fmt.Println(err)
			return
		}
	}
}
